﻿using System;
using System.ComponentModel.Composition;
using System.Linq;
using FinMkts.Messaging;

namespace FinMkts.DataProvider.Internal
{
    [Export(typeof(MarketDataProvider))]
    public class InternalMarketDataProvider : MarketDataProvider
    {
        [Import]
        public InternalPriceSerializer Serializer { get; set; }

        [Import]
        public Subscriber Subscriber { get; set; }

        public override IObservable<MarketPrice> GetPrice(string code)
        {
            var path = "Price." + code;
            return Subscriber
                .Where(message => message.Topic.StartsWith(path))
                .Select(message => (MarketPrice)Serializer.Deserialize(message.Body));
        }
    }
}
